<?php

//======================================================================
// CATEGORY LARGE FONT
//======================================================================

/*
-- ----------------------------
-- Table structure for test_deeploop
-- ----------------------------
DROP TABLE IF EXISTS `test_deeploop`;
CREATE TABLE `test_deeploop` (
  `id` int(4) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增主键',
  `catname` varchar(30) DEFAULT NULL COMMENT '分类名称',
  `pid` int(4) unsigned DEFAULT NULL COMMENT '父id',
  `catorder` int(2) unsigned DEFAULT NULL COMMENT '同级排序',
  `createtime` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of test_deeploop
-- ----------------------------
INSERT INTO `test_deeploop` VALUES ('1', '新闻', '0', '0', null);
INSERT INTO `test_deeploop` VALUES ('2', '图片', '0', '0', null);
INSERT INTO `test_deeploop` VALUES ('3', '国内新闻', '1', '0', null);
INSERT INTO `test_deeploop` VALUES ('4', '国际新闻', '1', '0', null);
INSERT INTO `test_deeploop` VALUES ('5', '北京新闻', '3', '0', null);
INSERT INTO `test_deeploop` VALUES ('6', '英伦新闻', '4', '0', null);
INSERT INTO `test_deeploop` VALUES ('7', '风景图片', '2', '0', null);
INSERT INTO `test_deeploop` VALUES ('8', '人物图片', '2', '0', null);
INSERT INTO `test_deeploop` VALUES ('9', '国外风景', '7', '0', null);
INSERT INTO `test_deeploop` VALUES ('10', '风云人物', '8', '0', null);

*/

require_once ('./class/MysqliDb.php');
$db = new MysqliDb (Array (
                'host' => '127.0.0.1',
                'username' => 'root', 
                'password' => 'root',
                'db'=> 'test',
                'port' => 3306,
                'charset' => 'utf8'));
                
/*===========================
插入数据
$data = Array (
    Array("id" => 1,"pid" => 0,"catname" => '新闻',"catorder" => 0),
    Array("id" => 2,"pid" => 0,"catname" => '图片',"catorder" => 0),
    Array("id" => 3,"pid" => 1,"catname" => '国内新闻',"catorder" => 0),
    Array("id" => 4,"pid" => 1,"catname" => '国际新闻',"catorder" => 0),
    Array("id" => 5,"pid" => 3,"catname" => '北京新闻',"catorder" => 0),
    Array("id" => 6,"pid" => 4,"catname" => '英伦新闻',"catorder" => 0),
    Array("id" => 7,"pid" => 2,"catname" => '风景图片',"catorder" => 0),
    Array("id" => 8,"pid" => 2,"catname" => '人物图片',"catorder" => 0),
    Array("id" => 9,"pid" => 7,"catname" => '国外风景',"catorder" => 0),
    Array("id" => 10,"pid" => 9,"catname" => '风云人物',"catorder" => 0),
);
//多记录插入
$ids = $db->insertMulti('test_deeploop', $data);
if(!$ids) {
    echo 'insert failed: ' . $db->getLastError();
} else {
    echo 'new users inserted with following id\'s: ' . implode(', ', $ids);
}
============================*/

  /**
   * 获取列表效果数组  
   */
  function getList($pid=0,$space_num=0){
    
    global $db;       //全局变量
    
    $space_num += 2;  //缩进空格数量递增2
    $res = array();   //返回的结果集
    
    $db->where("pid", $pid);              //初始查询条件
    $result = $db->get("test_deeploop");  //一级记录查询
    
    //递归查询
    foreach($result as $value){
      
      $value["catname"] = str_repeat("&nbsp;",$space_num) . "|--" . $value["catname"];
      
      //结果集添加记录并且根据记录$id查询子记录
      $res[]= $value;  
      $tmp = getList($value["id"],$space_num);
      
      //存在子记录,则合并查询结果集
      if($tmp){
        $res = array_merge($res,$tmp);
      }
    }
    return $res;
  }

  //输出查询结果
  $res = getList();
  
  //输出查询结果
  echo '<pre>';
  print_r($res);
  echo '</pre>';
  
  //输出显示函数
  function displayCate($pid=0,$select=0){
    
    $res = getList($pid);
    $str ="<select name='catname'>";
    foreach($res as $key=>$value){
      if($value['id']==$select){
        $selected = "selected";
      }else{
        $selected = "";   }
        
      $str .= "<option {$selected}>{$value['catname']}</option>";
    }
    $str .=("</select>");
    return $str;
  }
  //输出显示
  echo (displayCate(0,2));

?>